package cn.ming.leo.service;

import org.springframework.stereotype.Service;

import com.avaje.ebean.Ebean;
import com.avaje.ebean.ExpressionList;
import com.avaje.ebean.PagedList;

import cn.ming.leo.bean.Box;
import cn.ming.leo.utils.Message;
import cn.ming.leo.utils.PageListModel;

@Service
public class BoxService {

	public Message<Box> getById(int id) {
		return new Message<Box>(Box.find.byId("" + id));
	}

	public Message<String> updateStatus(int id, int status) {
		Box box = Box.find.byId("" + id);
		box.setStatus(status);
		box.update();
		return new Message<String>("success");
	}

	public Message<PageListModel<Box>> findAll(Integer pageIndex, Integer pageSize) {
		PagedList<Box> pl;
		ExpressionList<Box> pression = Ebean.find(Box.class).where();

		pageIndex = (pageIndex == null) ? 0 : pageIndex - 1;
		pageSize = (pageSize == null) ? 10 : pageSize;

		pl = pression.order().desc("create_time").findPagedList(pageIndex, pageSize);

		PageListModel<Box> pageListModel = new PageListModel<Box>(pl);

		return new Message<PageListModel<Box>>(pageListModel);
	}

}
